// TODO: CODE REVIEW & CLEANUP!
//
// AlbumButtonFace.cs
//
// Implements AlbumButtonFace and related types.
//

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using HomeUX.ClientControlSupport;
using HomeUX.Controls;

namespace HomeUX.MusicClientLibrary
{

[TemplatePart(Name = LayoutRootName, Type = typeof(Panel))]
[TemplatePart(Name = AlbumNameTextBlockName, Type = typeof(TextBlock))]
[TemplatePart(Name = NoAlbumArtIconName, Type = typeof(NoAlbumArtIcon))]
[TemplatePart(Name = AlbumArtImageName, Type = typeof(Image))]
public class AlbumButtonFace : Control, IControlHelperConnector
{
    const string LayoutRootName = "LayoutRoot";
    const string AlbumNameTextBlockName = "AlbumNameTextBlock";
    const string NoAlbumArtIconName = "NoAlbumArtIcon";
    const string AlbumArtImageName = "AlbumArtImage";

    public static readonly DependencyProperty AlbumNameProperty =
        DependencyProperty.Register("AlbumName", typeof(string), typeof(AlbumButtonFace),
            null/*new PropertyMetadata(AlbumButtonFace.OnAlbumNamePropertyChanged)*/);

    public static readonly DependencyProperty AlbumArtProperty =
        DependencyProperty.Register("AlbumArt", typeof(ImageSource), typeof(AlbumButtonFace),
            new PropertyMetadata(AlbumButtonFace.OnAlbumArtPropertyChanged));

    Panel _layoutRoot;
    TextBlock _albumNameTextBlock;
    NoAlbumArtIcon _noAlbumArtIcon;
    Image _albumArtImage;

    public string AlbumName
    {
        get
        {
            return (string)GetValue(AlbumNameProperty);
        }
        set
        {
            SetValue(AlbumNameProperty, value);
        }
    }

    public ImageSource AlbumArt
    {
        get
        {
            return (ImageSource)GetValue(AlbumArtProperty);
        }
        set
        {
            SetValue(AlbumArtProperty, value);
        }
    }

    public AlbumButtonFace() 
    {
        DefaultStyleKey = typeof(AlbumButtonFace);
    }

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        ControlHelper.GetRequiredTemplateChild(this, LayoutRootName, out _layoutRoot);
        ControlHelper.GetRequiredTemplateChild(this, AlbumNameTextBlockName, out _albumNameTextBlock);
        ControlHelper.GetRequiredTemplateChild(this, NoAlbumArtIconName, out _noAlbumArtIcon);
        ControlHelper.GetRequiredTemplateChild(this, AlbumArtImageName, out _albumArtImage);

        _albumArtImage.Source = AlbumArt;

        OnAlbumArtPropertyChanged();
	}

    static void OnAlbumArtPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var target = (AlbumButtonFace)d;
        if (target._albumArtImage != null)
            target.OnAlbumArtPropertyChanged();
    }

    void OnAlbumArtPropertyChanged()
    {
        // if album art is not available, show the "no album art" icon, and vice versa
        _noAlbumArtIcon.Visibility =
            (AlbumArt != null) ? Visibility.Collapsed : Visibility.Visible;
    }

	// IControlHelperConnector implementation

    Control IControlHelperConnector.GetControl()
    {
        return this;
    }

    DependencyObject IControlHelperConnector.GetTemplateChild(string childName)
    {
        return GetTemplateChild(childName);
    }
}

}

